#include "view_system.h"
#include "prj_typedef.h"
#include "blecom.h"
#include "display_ctrl.h"
#include "view_abstract.h"
#include "platform_assert.h"
#include "app_trace.h"
#include "motor.h"
#include "prj_config.h"
#include "display_interface.h"
#include "lfs_file_manager_auto.h"
#include "stopwatch.h"
#include "heart_rate_interface.h"
#include "rtc.h"
#include "app_trace.h"
#include "m_product_test.h"
#include "battery_ctrl.h"
#include "view_system.inc"
#include "heart_rate/view_heart_rate.h"
#include "arabic_reshaper.h"
#include "utf8_to_unicode.h"
#include "common/common.h"
#define P_POWEROFF_BYEBYE_X_POS 10
#define P_POWEROFF_BYEBYE_Y_POS 69
#define P_POWERON_HELLO_X_POS 10
#define P_POWERON_HELLO_Y_POS 69

#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)
void view_power_on_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        if (product_test_state_get() != PRODUCT_TEST_STATE_TEST_ASSEM)
        {
            display_show(view_time, DISP_MODE_NORMAL);
            return;
        }
        disp_ctrl.auto_refresh_time = 1000;
        if (disp_ctrl.auto_refresh_count >= 1)
        {
            // #warning debug
            display_show(view_time, DISP_MODE_DOWN_MOVE);
            return;
        }
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
    }
    draw_on_chip_unzip_gradients_image_with_back_ground(0, 0, NULL, RGB565_COLOR_WHITE, 0x1ea0, 0x0659);
    draw_on_chip_unzip_gradients_image_with_back_ground(P_POWERON_HELLO_X_POS, P_POWERON_HELLO_Y_POS, g_msg_hello_60x22, RGB565_COLOR_WHITE, 0x1ea0, 0x0659);
}

void view_power_off_animation_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        if (disp_ctrl.auto_refresh_count >= 1)
        {
            display_show(DISP_TYPE_CLOSE_OLED, DISP_MODE_NORMAL);
            return;
        }
    }
    draw_on_chip_unzip_gradients_image_with_back_ground(0, 0, NULL, RGB565_COLOR_WHITE, 0x073c, 0x0319);
    draw_on_chip_unzip_gradients_image_with_back_ground(P_POWEROFF_BYEBYE_X_POS, P_POWEROFF_BYEBYE_Y_POS, g_msg_byebye_60x22, RGB565_COLOR_WHITE, 0x059b, 0x04da);
}
bool view_power_on_event(event_param_t param)
{
    if (param.event == BUTTON_EVENT_LONG_PRESS
#ifdef USE_TOUCHPAD
        || param.event == BUTTON_EVENT_TP_TAP_LONG
#endif
    )
    {
        power_on();
        motor_start(500);
        display_show(view_power_on_animation, DISP_MODE_NORMAL);
        return true;
    }
    return false;
}
void view_heart_rate_test_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        disp_ctrl.com_data.i_temp[0] = 30 - disp_ctrl.auto_refresh_count / 2;
        if (disp_ctrl.com_data.i_temp[0] < 0)
        {
            disp_ctrl.com_data.i_temp[0] = 0;
        }
        if (disp_ctrl.auto_refresh_count > 35 * 2)
        {
            display_show_type_recover();
            return;
        }
        disp_ctrl.com_data.i_temp[1] = hr_if_band_wear();
        if (!disp_ctrl.com_data.i_temp[1])
        {
            //!未佩戴
            if (disp_ctrl.com_data.i_temp[4] == -1)
            {
                disp_ctrl.auto_refresh_count = disp_ctrl.auto_refresh_count;
                LOG("no wear start time [%d]", disp_ctrl.auto_refresh_count);
            }
            else
            {
                if (disp_ctrl.auto_refresh_count - disp_ctrl.com_data.i_temp[4] > 3 * 2)
                {
                    LOG("no wear timeout [%d]", disp_ctrl.auto_refresh_count - disp_ctrl.com_data.i_temp[4]);
                    display_show_type_recover();
                    return;
                }
            }
        }
        else
        {
            disp_ctrl.com_data.i_temp[4] = -1;
        }
    }

    view_heart_rate_param const param = {
        .is_wear = disp_ctrl.com_data.i_temp[1],
        .second = disp_ctrl.com_data.i_temp[0],
    };
    view_heart_rate_animation(&param);
}
void view_heart_rate_result_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;

        disp_ctrl.com_data.i_temp[0] = app_health_test_result[0];
        if (disp_ctrl.auto_refresh_count > 5 * 2)
        {
            display_show_type_recover();
            return;
        }
    }
    view_heart_rate_param const param = {
        .is_wear = true,
        .heart_rate = disp_ctrl.com_data.i_temp[0],
        .second = 0,
    };
    view_heart_rate_animation(&param);
}
void view_screen_test_display(void)
{
    draw_rectangle(0, 0, OLED_SIZE_X - 1, OLED_SIZE_Y - 1, RGB565_COLOR_WHITE);
}

bool view_system_display(int view)
{
    switch (view)
    {
    case view_heart_rate_test:
        view_heart_rate_test_display();
        return true;
    case view_heart_rate_result:
        view_heart_rate_result_display();
        return true;
    case view_power_on_animation:
        view_power_on_display();
        return true;
    case view_power_off_animation:
        view_power_off_animation_display();
        return true;
    case view_untested:
        view_untested_display();
        return true;
    case view_screen_test:
        view_screen_test_display();
        return true;
    }
    return false;
}
void view_untested_display(void)
{
    draw_device_info();
}

bool view_screen_test_event(event_param_t param)
{
    switch (param.event)
    {
    case BUTTON_EVENT_SHORT_PRESS:
        display_show(view_time, DISP_MODE_DOWN_MOVE);
        return true;
    }
    return false;
}
bool view_heart_rate_test_event(event_param_t param)
{
    switch (param.event)
    {
    case event_view_entry:
        disp_ctrl.auto_close_delay = 40000;
        return true;
    case event_view_exit:
        APPhealth_Stop();
        hr_test_stop(HR_CTRL_TYPE_HR_TEST);
        return true;
    case event_ble_disconnect:
        // case event_move_level_up:
        display_show_type_recover();
        LOG("view_heart_rate_test cancel");
        return true;
    }
    return false;
}
bool view_heart_rate_test_result_event(event_param_t param)
{
    switch (param.event)
    {
    case event_view_entry:
        disp_ctrl.auto_close_delay = 10000;
        stopwatch_start();
        LOG("view_heart_rate_test entry");
        return true;
    case event_view_exit:
        stopwatch_stop();
        LOG("view_heart_rate_test exit");
        return true;
    }
    return false;
}
bool view_system_event(int view, event_param_t param)
{
    bool result = false;
    switch (view)
    {
    case view_screen_test:
        result = view_screen_test_event(param);
        break;
    case view_heart_rate_test:
        result = view_heart_rate_test_event(param);
        break;
    case view_heart_rate_result:
        result = view_heart_rate_test_result_event(param);
        break;
    }
    return result;
}
